package com.itheima.goods.service.impl;


import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.entity.Result;
import com.itheima.entity.StatusCode;
import com.itheima.goods.mapper.BrandMapper;
import com.itheima.goods.service.BrandService;
import com.itheima.pojo.Brand;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * @author TSV
 * @description 品牌接口实现类
 * @date 2021/8/16
 */
@Service("brandService")
public class BrandServiceImpl extends ServiceImpl<BrandMapper,Brand> implements BrandService {


    /**
     * 功能描述 查询所有品牌
     *
     * @param
     * @return com.itheima.entity.Result
     * @auther TSV
     * @date 2021/8/16
     */
    @Override
    public Result findAll() {
        // 调用Mapper接口方法查询所有品牌
        List<Brand> list = list();
        // 返回结果
        return new Result(StatusCode.OK, true, "", list);
    }

    /**
     * 功能描述 添加品牌
     *
     * @param brand
     * @return java.lang.Boolean
     * @auther TSV
     * @date 2021/8/16
     */
    @Override
    public Boolean add(Brand brand) {
        return save( brand );
    }

    /**
     *功能描述 分页查询
     * @auther TSV
     * @date 2021/8/16
     * @param page
     * @param size
     * @return com.itheima.entity.Result
     */
    @Override
    public Result findPage(Long page, Long size) {
        IPage<Brand> brandPage = new Page<>( page, size );
        page(brandPage);
        List<Brand> records = brandPage.getRecords();
        return new Result(StatusCode.OK, true, "", records);
    }
    /**
     *功能描述 根据id查询品牌
     * @auther TSV
     * @date 2021/8/16
     * @param id
     * @return com.itheima.entity.Result
     */
    @Override
    public Result findById(Integer id) {
        Brand brand = getById( id );
        return new Result(StatusCode.OK, true, "", brand);
    }
    /**
     *功能描述 根据id修改品牌
     * @auther TSV
     * @date 2021/8/16
     * @param id
     * @param brand
     * @return com.itheima.entity.Result
     */
    @Override
    public Boolean updateBrand(Integer id, Brand brand) {
        brand.setId(id);
        LambdaUpdateWrapper<Brand> wrapper = new LambdaUpdateWrapper<Brand>().eq( Brand::getId, brand.getId() );
        return update( brand,wrapper );
    }
    /**
     *功能描述 删除品牌
     * @auther TSV
     * @date 2021/8/16
     * @param id
     * @return java.lang.Boolean
     */
    @Override
    public Boolean deleteBrand(Integer id) {
        boolean b = removeById( id );
        return b;
    }
}
